/*
 * @Author: chengsl
 * @Date: 2023-05-09 08:47:52
 * @LastEditors: chengsl
 * @LastEditTime: 2023-05-10 15:02:19
 * @Description: file content
 */

'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
// const HardSourceWebpackPlugin = require('hard-source-webpack-plugin')
const timeStamp = new Date().getTime()
const sysName = defaultSettings.systemEnName
function resolve(dir) {
  return path.join(__dirname, dir)
}

const target = 'http://192.168.13.159:9095' // 内网

const name = defaultSettings.title // 网址标题
const port = defaultSettings.port // 端口配置
module.exports = {
  publicPath: process.env.NODE_ENV === 'development'
    ? '/'
    : '/microapp-ui',
  outputDir: 'dist',
  assetsDir: 'static',
  transpileDependencies: ['vuex-persist', 'element-ui'],
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    disableHostCheck: true, // webpack4.0 开启热更新
    port: port,
    open: false, // 启动项目以后自动打开浏览器
    hot: true, // 模块热替换(HMR - hot module replacement)功能会在应用程序运行过程中，替换、添加或删除 模块，而无需重新加载整个页面。主要是通过以下几种方式，来显著加快开发速度
    hotOnly: false, // hot 和 hotOnly 的区别是在某些模块不支持热更新的情况下，前者会自动刷新页面，后者不会刷新页面，而是在控制台输出热更新失败
    overlay: {
      warnings: false,
      errors: true
    },
    headers: {
      'Access-Control-Allow-Origin': '*'
    },
    proxy: {
      '/microapp-api': {
        target,
        changeOrigin: true,
        pathRewrite: {
          '^/microapp-api': ''
        }
      },
      '/sys': {
        // target: 'http://192.168.13.148:40020', // 研发
        target: 'http://192.168.13.154:10087', // 测试
        logLevel: 'debug',
        ws: true,
        changOrigin: true // 允许跨域
      },
    }
  },
  configureWebpack: {
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    performance: {
      hints: false
    },
    output: {
      // 覆写 filename 或 chunkFilename 时，assetsDir 会被忽略. 因此 js 和 css 文件不会被放入 static 文件夹，需手动修改
      chunkFilename: `static/js/[id].[chunkhash].${timeStamp}.js`,
      library: `${sysName}`, // 微应用的包名，这里与主应用中注册的微应用名称一致
      libraryTarget: 'umd', // 将你的 library 暴露为所有的模块定义下都可运行的方式
      jsonpFunction: `webpackJsonp_${sysName}` // 按需加载相关，设置为 webpackJsonp_vue-projec 即可
    },
    plugins: [
      // new HardSourceWebpackPlugin({
      //   test: /mini-css-extract-plugin[\\/]dist[\\/]loader/
      // })
    ]
  },
  css: {
    extract: { // 打包后css文件名称添加时间戳
      // 覆写 filename 或 chunkFilename 时，assetsDir 会被忽略. 因此 js 和 css 文件不会被放入 static 文件夹，需手动修改
      filename: `static/css/[name].${timeStamp}.css`,
      chunkFilename: `static/css/chunk.[id].${timeStamp}.css`
    }
  },
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
    config.module
      .rule('fonts')
      .use('url-loader')
      .loader('url-loader')
      .options({})
      .end()
    config.module
      .rule('images')
      .use('url-loader')
      .loader('url-loader')
      .options({})
      .end()
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        // options.compilerOptions = {
        //   preserveWhitespace: true
        // }
        options.compilerOptions = {
          ...options.compilerOptions,
          // isCustomElement: tag => tag.startsWith('el-'),
          preserveWhitespace: true
        }
        return options
      })
      .end()
    config
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )
    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                // elementUI: {
                //   name: 'chunk-elementUI', // split elementUI into a single package
                //   priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                //   test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                // },
                elementPlus: {
                  name: 'chunk-elementPlus', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-plus(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
